import router from '@/router/index.tsx'
// 过滤hidden的菜单
const filterHidden = (list, isHidden = true) => {
    let data = list.find(item => item.name === 'index').children
    if (isHidden) {
        return data.filter(item => !item.hidden)
    } else {
        return data
    }
}
export const getMenus = () => {
    // 1. 使用reduce创建分组对象
    const groups = filterHidden(router.routes).reduce((acc, item) => {
        const key = item.menuName; // 获取当前对象的menuName值
        // 如果分组不存在则初始化
        if (!acc[key]) {
            acc[key] = [];
        }
        // 将当前对象加入对应分组
        acc[key].push(item);
        return acc;
    }, {});
    return Object.entries(groups).reduce((res, ite) => {
        res.push({
            label: ite[0],
            key: ite[0],
            children: ite[1].map(ite2 => {
                return {
                    label: ite2.name,
                    key: ite2.path,
                    fatherkey: ite[0],
                }
            }),
            icon: ite[1][0].icon
        })
        return res
    }, [])
}
// 根据 path 获取当前路由
export const getCurrentRoute = (pathname: string) => {
    return filterHidden(router.routes, false).find((item) => item.path === pathname)
}

function findObjectByKey(list, targetKey) {
    let obj = null;
    // 检查当前对象是否为非空对象
    list.forEach((item) => {
        if (item.key === targetKey) {
            obj = item;
        } else if (item.children) {
            obj = findObjectByKey(item.children, targetKey)
        }
    })
    return obj; // 未找到返回 null
}

// 根据 path 获取当前菜单
export const getCurrentMenus = (list, pathname: string) => {
    return  findObjectByKey(list, pathname)
}
export const IconMap = {
    '保单管理': '',
    '产品管理':'https://sqzbx-test.etib.cn/homePage/public/img/%E4%BA%A7%E5%93%81%E5%BA%93.bc417903.svg',
    'OA审批':''
}